package com.hp.bon.sgw.wh.utils;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.lang.StringUtils;

/**
 * 支付宝加密工具类
 * 
 * @author suxinj
 */
public class DESCryptoUtil {
	
	/*public static void main(String[] args) {
		String a="ue0RlBpL82D/5+5TZd7yf9fiPe3jVvW1ZC1KyIkFvf26G5eaxp7rD29oZwen9yStOn8ju1q1IUi7627j6w/XuM1ShCUBcEcStN90Rq0sOpTzAhkOg0EKhFOdcPpshCcWc/1tlKtq2dqhJa2uzmt8BAh+lLq4FiZs6TtafPwh6y9lNJC2Galh5xUo/ZndBHWmXpyjHHfdQadUybaZvBV+0+77caUyOmseY+mGFuysBBPw+4dNTk0MAH2lJQ27AxRQmeRVu2HiGLCp3uOYcc33l6Z1rxtqduuur5WUX4t1GTw373g/DInar8MrdtGSR5DOiB3ycqZiieTBK/STpXaHy60XpCWVaKwkFCYWgrefGrTm3DE106mR560nPya4o5HcLfbvQ1GBV9Xm3DE106mR58FtPY4GIkCc6dfJHvjZTu5g1UQnLv2VDLOozHCSEmv5GnTgN1Lo2bDvFWmk/s5QyYweedxkykpSNKua7JWkvjtIHYAPOAuY5ldTVNagiEEwD9GWB2SIf+/IzQSk84Ijl06b0l8Q6EgevQ9Pe1yMc1SwftqUxxLGz62G4IppV6kgENPIDJOGC4F36uokHdruecBjNxHwIIXYuTFmYjQisUxz0hR1T8Zu12dGaIWiK8yGNqUgoNMDbY0eXKcOvQ4EkgtB/7lpOjJ6lVEMplrz9yX9XQZ6K+CXO0CQSRCIDAxyCtZU5BUV62PpAwh4NrGOXVibpiuDIGT1wKf9WVIXDUdr4uSJ1L7hCg==";
		String key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCu6c1LYz3dIz44davVNZFFwmVOs2rpfZcbjPxMTeJVaXK+3TFl3D6Z2gZuDjsyEPTNtyd2Lu+CHthu4ciSnvGjUv+lLrFs8yriYXOhnbynLRQlU15ubQ/ypqL7HEESOZ71KVHE9RbwDOKsP7uA8m6JHMDW1YY1CTipemV3P7zONQIDAQAB";
		System.out.println(decrypt(a,key));
	}*/

	/**
	 * 采用DES 加/解 密
	 */
	private final static String DES = "DES";

	/**
	 * 编码方式
	 */
	private final static String CHARSET = "UTF-8";

	/**
	 * 加密数据
	 * 
	 * @param data
	 *            待加密的数据
	 * @param key
	 *            密钥
	 * @return 密文
	 */
	public static String encrypt(String data, String key ,String charset) {
		if(charset == null){
			charset = CHARSET;
		}
		byte[] bt = null;
		try {
			bt = encrypt(data.getBytes(charset), key.getBytes(charset));
			return (null == bt) ? "" : Base64.encode(bt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/***
	 * 加密数据
	 * 
	 * @param data
	 *            待加密数据
	 * @param key
	 *            密钥
	 * @return
	 * @throws Exception
	 */
	private static byte[] encrypt(byte[] data, byte[] key) {
		SecureRandom sr = new SecureRandom();
		DESKeySpec dks = null;
		Cipher cipher = null;
		try {
			dks = new DESKeySpec(key);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
			SecretKey securekey = keyFactory.generateSecret(dks);
			cipher = Cipher.getInstance(DES);
			cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
			return cipher.doFinal(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 解密数据
	 * 
	 * @param data
	 *            待解密的数据
	 * @param key
	 *            密钥
	 * @return
	 */
	public static String decrypt(String data, String key ,String charset) {
		if(charset == null){
			charset = CHARSET;
		}
		if (StringUtils.isBlank(data)) {
			return "";
		}
		byte[] buf;
		try {
			buf = Base64.decode(data);
			byte[] bt = decrypt(buf, key.getBytes(charset));
			return new String(bt, charset);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 解密
	 * 
	 * @param data
	 *            待解密数据
	 * @param key
	 *            密钥
	 * @return
	 * @throws Exception
	 */
	private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
		SecureRandom sr = new SecureRandom();
		DESKeySpec dks = new DESKeySpec(key);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
		SecretKey securekey = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance(DES);
		cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
		return cipher.doFinal(data);
	}
	
	
	
	public static void main(String[] args) {
		String str1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><message><head><version>1.0.1</version><instId>ALIPAY</instId><type>SignReq</type><msgId>-2386772353454029591</msgId><msgTime>20140523 10:08:19</msgTime><secId>0002</secId></head><body><userNumber>18986137973</userNumber><alertValue></alertValue><signType>SIGN</signType><businessType>BalanceAlert</businessType><signTime>20140523 10:08:19</signTime></body><signTag>UZCS/2Dgo2nuPuEFrYWyIs+/ZAdqQ8t6X4abKiu92OqroDsDWlOhGMh9y/DARbO9AEZ09ShzakiAAUA3pX0Ae6YVg510Hm5LBP55k163YMMvDYMgztcmgmhl57Jpd6t4nhbQe4xNln7N5Yr2eX+AE3gx3uJ3taTxhfXdfz9EAa4=</signTag></message>";
		String PUB_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLjMwWgWD2g4v2XOt39ukZ0Uxa1r/Kh4zMdXB6K5PY2fwwcipqEzycdnhfnf81V0oOcW2vVWpnFpDlsqNfDEPTaDxoCCHOOzQoi999q+biF14oJSSFZ71fefC9vxc1o2zl0CHkBD+DeE86+HxpaE2GEcs4yXSmdPN2AcKMYHBKrwIDAQAB";
		System.out.println(encrypt(str1,PUB_KEY ,"GBK") );//jiami
		System.out.println(decrypt(encrypt(str1,PUB_KEY ,"GBK"), PUB_KEY ,"GBK"));//jiemi
		
	}
	
	
	
	
	
	
	
	
}
